//
//  LGSportsFillInCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//

#import "LGRecoverySportsInputCell.h"

@interface LGRecoverySportsInputCell()<UITextViewDelegate>

@end

@implementation LGRecoverySportsInputCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        [self.contentView addSubview:self.textView];
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(5));
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(-viewPix(13));
            make.height.equalTo(@(viewPix(43)));
        }];
    }
    return  self;
}

-(void)setSection:(NSInteger)section{
    _section = section;
}

-(void)setMinHeight:(CGFloat)minHeight{
    _minHeight = minHeight;
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(minHeight));
    }];
}

-(void)setPlaceHolder:(NSString *)placeHolder{
    _placeHolder = placeHolder;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.textView.text = content;
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        self.textView.text = self.placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#999999"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
    [self resetTextViewHeight:content needUpdate:NO];

}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:self.placeHolder]){
        self.textView.text = @"";
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    if(textView.text.length == 0){
        self.textView.textColor = [UIColor colorWithString:@"#999999"];
        self.textView.text = self.placeHolder;
        
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

-(void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if (content.length > self.maxLength && ![content isEqualToString:self.placeHolder]) { // 输入的文字大于设定的长度  这里设置的是200个字符
        content = [self.textView.text substringToIndex:self.maxLength];
    }
//    NSString *colorStr = (content.length>0 && ![content isEqualToString:self.placeHolder])?@"#333333":@"#999999";
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) {// 没有高亮
            [self resetTextViewHeight:content needUpdate:YES];
        }
    }else{
        [self resetTextViewHeight:content needUpdate:YES];
    }
    
}
-(void)resetTextViewHeight:(NSString *)content needUpdate:(BOOL)need{
    NSString *colorStr = (content.length>0 && ![content isEqualToString:self.placeHolder])?@"#333333":@"#999999";
    if (content.length > self.maxLength && ![content isEqualToString:self.placeHolder]) { // 输入的文字大于设定的长度  这里设置的是200个字符
        content = [self.textView.text substringToIndex:self.maxLength];
    }
    if(content.length>0){
        self.textView.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(16),NSForegroundColorAttributeName:[UIColor colorWithString:colorStr]} lineSpacing:5];
    }
//    CGFloat viewHeight = self.textView.bounds.size.height;
    [self.textView sizeToFit];
    CGFloat newHeight = [self.textView sizeThatFits:CGSizeMake(Screen_W-viewPix(42), MAXFLOAT)].height+viewPix(5);
    NSLog(@">>>???>>>%.2f--%.2f",newHeight,self.minHeight);
    CGFloat viewHeight = (newHeight > self.minHeight) ? newHeight : self.minHeight;
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(viewHeight));
    }];
    if(![content isEqualToString:self.placeHolder] && self.delegate && [self.delegate respondsToSelector:@selector(inputCellEditingChanged:section:)]){
        [self.delegate inputCellEditingChanged:content section:self.section];
    }
    if(need == YES){
        UITableView *tableView = [self tableView];
        [tableView beginUpdates];
        [tableView endUpdates];
    }
}

-(UITableView *)tableView{
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}

-(UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]init];
        _textView.textContainerInset = UIEdgeInsetsMake(viewPix(10), viewPix(5), viewPix(5), viewPix(5));
        _textView.backgroundColor = [UIColor colorWithString:@"#EFEFEF"];
        _textView.tintColor = [UIColor colorWithString:@"#333333"];
        _textView.textColor = [UIColor colorWithString:@"#999999"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.layer.cornerRadius = 8;
        _textView.scrollEnabled = NO;
        _textView.font = LGFont(16);
        _textView.delegate = self;
    }
    return _textView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
